<?php 
/**
 * Класс, который реализовует Extjs Grid
 *
 */

class FRM_View_Ext_Tree extends FRM_View_Ext_Abstract
{
	/**
	 * Enter description here...
	 *
	 * @var int
	 */
	protected $postParam = "id_parent";
	
	/**
	 * Enter description here...
	 *
	 * @var int
	 */
	protected $url;
	
	/**
	 * Enter description here...
	 *
	 * @var int
	 */
	protected $rootTitle = "Menu";
	
	/**
	 * Enter description here...
	 *
	 * @var int
	 */
	protected $title = "Елементы";
		
	/**
	 * Enter description here...
	 *
	 * @var int
	 */
	protected $parentField = "id_parent";
		
	/**
	 * Enter description here...
	 *
	 * @param string $postParams
	 */
	public function setPostParams ($postParams){
		$this->postParams = $postParams;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $url
	 */
	public function setUrl ($url){
		$this->url = $url;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $url
	 */
	public function setRootTitle ($rootTitle){
		$this->rootTitle = $rootTitle;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $url
	 */
	public function setTitle ($title){
		$this->title = $title;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param string $parentField
	 */
	public function setParentField ($parentField){
		$this->parentField = $parentField;
	}
	
	public function render(){
		
		$this->preRender();
		
		// Ext.OnReady -----------------------
		PhpExt_Ext::AddOnReady(
			PhpExt_Javascript::inlineStm("var tree = new Ext.tree.TreePanel( {
		      animate:true,
        containerScroll: true,  loader : new Ext.tree.TreeLoader( {
		         dataUrl : '".$this->url."', listeners:{beforeload:function(treeLoader, node) {
        this.baseParams.".$this->parentField." = node.attributes.".$this->parentField.";
    }}}
		      ),rootVisible : true, region : 'west', width : 200, split : true, title : '".$this->title."', autoScroll : true,
		      margins : '3 0 3 3'}
		   );
		   var root = new Ext.tree.AsyncTreeNode( {
		      text : '".$this->rootTitle."', draggable : false, expanded : true, id : '0'}
		   );
		   
		   tree.setRootNode(root);
		   tree.getRootNode().select();
		   tree.on('click', function(node) {
		      if(node) {
		         store.baseParams.".$this->parentField." = node.attributes.".$this->parentField.";
		         //editForm.form.get('$this->parentField') = node.attributes.".$this->parentField.";
		         store.load();
		      }
		      });
		      new Ext.Panel({id : '".self::$controller."-tree-id', layout : 'border', items : [tree, new Ext.Panel( {
                  region : 'center', margins : '3 0 3 0', layout : 'fit', items : grid}
               )]});
		      
		      ")
		);
		
	}
}